package com.acce.gxsc.page.work;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.MainActivity;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.loaclBean.WorkTimeBean;
import com.acce.gxsc.model.netBean.DutyBean;
import com.acce.gxsc.model.netBean.NetWorkAreaBean;
import com.acce.sdk.User;
import com.acce.sdk.base.main.Category;
import com.acce.sdk.base.main.Theme;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.model.LoginModel;
import com.acce.sdk.user.login.LoginActivity;
import com.acce.sdk.utils.SharePreferencesManager;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.TimeUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.adapterlv.CommonAdapter;
import com.acce.sdk.widget.adapterlv.ViewHolder;
import com.acce.sdk.widget.lemonAlert.LemonAlert;
import com.acce.sdk.widget.lemonAlert.LemonAlertAction;
import com.acce.sdk.widget.lemonAlert.LemonAlertInfo;
import com.acce.sdk.widget.lemonAlert.LemonAlertView;
import com.acce.sdk.widget.lemonAlert.interfaces.LemonAlertActionDelegate;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import static com.acce.gxsc.common.Constants.ID_0;
import static com.acce.gxsc.common.Constants.ID_1;
import static com.acce.gxsc.common.Constants.ID_2;
import static com.acce.gxsc.common.Constants.ID_3;
import static com.acce.gxsc.common.Constants.ID_4;
import static com.acce.gxsc.common.Constants.ID_5;
import static com.acce.gxsc.common.Constants.ID_6;
import static com.acce.gxsc.common.Constants.ID_7;
import static com.acce.gxsc.common.Constants.ID_8;
import static com.acce.gxsc.common.Constants.categoryList;
import static com.acce.sdk.BaseApp.context;

/**
 * Created by huxley on 2017/5/26.
 * 登录后选择工作以及工作时间的页面
 */

public class WorkActivity extends BaseMvpActivity<WorkView, WorkPresenter> implements WorkView {

    //    @Bind(R.id.toolbar)
//    AcceBar toolbar;
    @Bind(R.id.tv_content)
    TextView mTvContent;
    @Bind(R.id.spinner_place)
    Spinner mSpinnerPlace;
    @Bind(R.id.spinner_time)
    Spinner mSpinnerTime;
    @Bind(R.id.btn_work)
    Button mBtnWork;
    @Bind(R.id.btn_again_work)
    Button mBtnAgainWork;
    @Bind(R.id.btn_work_list)
    Button mBtnWorkList;
    @Bind(R.id.tv_hello_name)
    TextView mTvHelloName;
    @Bind(R.id.btn_work_out)
    Button mBtnWorkOut;

    DutyBean mDutyBean;
    CommonAdapter workAreaAdapter;
    CommonAdapter workTimeAdapter;
    List<WorkTimeBean> workTimeList = new ArrayList<>();
    ArrayList<NetWorkAreaBean> mWorkAreaList = new ArrayList<>();

    @Override
    protected WorkPresenter createPresenter() {
        return new WorkPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_work);
        ButterKnife.bind(this);
        init();
    }


    private void init() {
        mTvHelloName.setText(MessageFormat.format("您好，{0}：", User.getInstance().getRealName()));
        mSpinnerTime.setAdapter(workTimeAdapter = new CommonAdapter<WorkTimeBean>(this, R.layout.input_spinner_item, workTimeList) {
            @Override
            protected void convert(ViewHolder viewHolder, WorkTimeBean workTimeBean, int i) {
                viewHolder.setText(R.id.tv_content, workTimeBean.workTimeName);
            }
        });
        mSpinnerPlace.setAdapter(workAreaAdapter = new CommonAdapter<NetWorkAreaBean>(this, R.layout.input_spinner_item, mWorkAreaList) {
            @Override
            protected void convert(ViewHolder viewHolder, NetWorkAreaBean gateBean, int i) {
                viewHolder.setText(R.id.tv_content, gateBean.gateName);
            }
        });

        mSpinnerPlace.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                switch (gateNo) {
//                    case "501"://新区市场监察部
//                        workTimeList.add(new WorkTimeBean("1", "早班: 07:00 ~ 15:00"));
//                        workTimeList.add(new WorkTimeBean("2", "中班: 15:00 ~ 23:00"));
//                        workTimeList.add(new WorkTimeBean("3", "晚班: 23:00 ~ 次日07:00"));
//                        categoryList.add(new Category(ID_6, "市场监察", R.mipmap.ic_7, Theme.green));
//                        categoryList.add(new Category(ID_7, "监察交接班报表", R.mipmap.ic_8, Theme.yellow));
//                        break;
//                    case "101"://江南市场1#门登记处
//                        workTimeList.add(new WorkTimeBean("1", "早班: 06:00 ~ 14:00"));
//                        workTimeList.add(new WorkTimeBean("2", "中班: 14:00 ~ 22:00"));
//                        workTimeList.add(new WorkTimeBean("3", "晚班: 22:00 ~ 次日06:00"));
//                        categoryList.add(new Category(ID_0, "1#门车辆进场登记", R.mipmap.ic_1, Theme.green));
//                        categoryList.add(new Category(ID_1, "1#门进场登记当班记录", R.mipmap.ic_2, Theme.yellow));
//                        categoryList.add(new Category(ID_2, "1#门车辆预报备列表", R.mipmap.ic_3, Theme.spe));
//                        categoryList.add(new Category(ID_4, "1#门交接班汇总报表", R.mipmap.ic_5, Theme.red));
//                        break;
//                    case "401"://新区2#离场检查
//                        workTimeList.add(new WorkTimeBean("1", "早班: 06:00 ~ 14:00"));
//                        workTimeList.add(new WorkTimeBean("2", "中班: 14:00 ~ 22:00"));
//                        workTimeList.add(new WorkTimeBean("3", "晚班: 22:00 ~ 次日06:00"));
//                        categoryList.add(new Category(ID_3, "车辆离场登记", R.mipmap.ic_4, Theme.blue));
//                        categoryList.add(new Category(ID_5, "车辆离场交接班", R.mipmap.ic_6, Theme.purple));
//                        break;
//                    case "901":
//                        workTimeList.add(new WorkTimeBean("1", "早班: 06:00 ~ 14:00"));
//                        workTimeList.add(new WorkTimeBean("2", "中班: 14:00 ~ 22:00"));
//                        workTimeList.add(new WorkTimeBean("3", "晚班: 22:00 ~ 次日06:00"));
//                        categoryList.add(new Category(ID_0, "1#门车辆进场登记", R.mipmap.ic_1, Theme.green));
//                        categoryList.add(new Category(ID_1, "1#门进场登记当班记录", R.mipmap.ic_2, Theme.yellow));
//                        categoryList.add(new Category(ID_2, "1#门车辆预报备列表", R.mipmap.ic_3, Theme.spe));
//                        categoryList.add(new Category(ID_3, "车辆离场登记", R.mipmap.ic_4, Theme.blue));
//                        categoryList.add(new Category(ID_6, "市场监察", R.mipmap.ic_7, Theme.green));
//                        categoryList.add(new Category(ID_4, "1#门交接班汇总报表", R.mipmap.ic_5, Theme.red));
//                        categoryList.add(new Category(ID_5, "车辆离场交接班", R.mipmap.ic_6, Theme.purple));
//                        categoryList.add(new Category(ID_7, "监察交接班报表", R.mipmap.ic_8, Theme.yellow));
//                        break;
//                }
                mPresenter.loadPositionList(i);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        mPresenter.loadPositionList(0);
//        toolbar.setTitleText("");
    }

    @OnClick({R.id.btn_again_work, R.id.btn_work_list, R.id.btn_work, R.id.btn_work_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_again_work:
                mPresenter.workAgain(mDutyBean.dutyId);
                break;
            case R.id.btn_work_list:
                LemonAlert.getInformationAlert("退出登录", "您确定要退出登录吗？")
                        .addAction(new LemonAlertAction("取消", new LemonAlertActionDelegate() {
                            @Override
                            public void onClick(LemonAlertView alertView, LemonAlertInfo alertInfo, LemonAlertAction alertAction) {
                                alertView.hide();
                            }
                        }))
                        .addAction(new LemonAlertAction("退出登录", Color.RED, new LemonAlertActionDelegate() {
                            @Override
                            public void onClick(LemonAlertView alertView, LemonAlertInfo helloInfo, LemonAlertAction helloAction) {
                                alertView.hide();
                                LoginModel loginModel = new LoginModel();
                                loginModel.exitLogin(WorkActivity.this);

                                Intent loginStarter = new Intent(WorkActivity.this, LoginActivity.class);
                                startActivity(loginStarter);
                                finish();
                            }
                        })).show(WorkActivity.this);
                break;
            case R.id.btn_work:
                if (mWorkAreaList != null && mWorkAreaList.size() <= 0) {
                    ToastUtils.show("该用户没有加入部门,不能上岗!");
                    return;
                }
                try {
                    String gateNo = mWorkAreaList.get(mSpinnerPlace.getSelectedItemPosition()).gateNo;
                    String workNo = workTimeList.get(mSpinnerTime.getSelectedItemPosition()).workTimeNo;
                    mPresenter.startWork(gateNo, workNo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_work_out:
                exit();
                break;
        }
    }

    private void exit() {

        if (mDutyBean == null || mDutyBean.dutyId == null) {
            ToastUtils.show("没有查到上班信息");
            return;
        }
        WorkModel.getInstance().endWork(mDutyBean.dutyId).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                showProgressDialog("加载中...", true);
            }

            @Override
            public void onNext(ResultBean<Object> datas) {
                ToastUtils.show("下班成功");
                Intent loginStarter = new Intent(WorkActivity.this, LoginActivity.class);
                startActivity(loginStarter);
                SharePreferencesManager spm = new SharePreferencesManager(context);
                spm.writeBooleanSharedPreferences("isLogin", Boolean.FALSE);
                spm.writeStringSharedPreferences("password", null);
                finish();
            }

            @Override
            public void onCompleted() {
                hideProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                hideProgressDialog();
            }
        });
    }

    @Override
    public void startWorkSuccess(DutyBean dutyBean) {
        setIcon(mWorkAreaList.get(mSpinnerPlace.getSelectedItemPosition()).deptId);
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("duty", dutyBean);
        startActivity(intent);
        finish();
    }

    private void setIcon(String gateNo) {
        if (StringUtils.isEmpty(gateNo)) {
            return;
        }
        if (categoryList == null) {
            categoryList = new ArrayList<>();
        } else {
            categoryList.clear();
        }
        switch (gateNo) {
//            case "501"://新区市场监察部
//                categoryList.add(new Category(ID_6, "市场监察", R.mipmap.ic_7, Theme.green));
//                categoryList.add(new Category(ID_7, "监察交接班报表", R.mipmap.ic_8, Theme.yellow));
//                break;
//            case "101"://江南市场1#门登记处
//                categoryList.add(new Category(ID_0, "1#门车辆进场登记", R.mipmap.ic_1, Theme.green));
//                categoryList.add(new Category(ID_1, "1#门进场登记当班记录", R.mipmap.ic_2, Theme.yellow));
//                categoryList.add(new Category(ID_2, "1#门车辆预报备列表", R.mipmap.ic_3, Theme.spe));
//                categoryList.add(new Category(ID_4, "1#门交接班汇总报表", R.mipmap.ic_5, Theme.red));
//                break;
//            case "401"://新区2#离场检查
//                categoryList.add(new Category(ID_3, "车辆离场登记", R.mipmap.ic_4, Theme.blue));
//                categoryList.add(new Category(ID_5, "车辆离场交接班", R.mipmap.ic_6, Theme.purple));
//                break;
            default:

            case "501"://新区市场监察部
                categoryList.add(new Category(ID_6, "市场监察", R.mipmap.ic_7, Theme.green));
                categoryList.add(new Category(ID_7, "监察交接班报表", R.mipmap.ic_8, Theme.yellow));
                categoryList.add(new Category(ID_8, "二次离场登记", R.mipmap.ic_4, Theme.red));
                break;
            case "101"://江南市场1#门登记处
                categoryList.add(new Category(ID_0, "1#门车辆进场登记", R.mipmap.ic_1, Theme.green));
                categoryList.add(new Category(ID_1, "1#门进场登记当班记录", R.mipmap.ic_2, Theme.yellow));
                categoryList.add(new Category(ID_2, "1#门车辆预报备列表", R.mipmap.ic_3, Theme.spe));
                categoryList.add(new Category(ID_4, "1#门交接班汇总报表", R.mipmap.ic_5, Theme.red));
                categoryList.add(new Category(ID_8, "二次离场登记", R.mipmap.ic_4, Theme.red));
                break;
            case "401"://新区2#离场检查
                categoryList.add(new Category(ID_3, "车辆离场登记", R.mipmap.ic_4, Theme.blue));
                categoryList.add(new Category(ID_5, "车辆离场交接班", R.mipmap.ic_6, Theme.purple));
                categoryList.add(new Category(ID_8, "二次离场登记", R.mipmap.ic_4, Theme.red));
                break;
            case "901":
                categoryList.add(new Category(ID_0, "1#门车辆进场登记", R.mipmap.ic_1, Theme.green));
                categoryList.add(new Category(ID_1, "1#门进场登记当班记录", R.mipmap.ic_2, Theme.yellow));
                categoryList.add(new Category(ID_2, "1#门车辆预报备列表", R.mipmap.ic_3, Theme.spe));
                categoryList.add(new Category(ID_3, "车辆离场登记", R.mipmap.ic_4, Theme.blue));
                categoryList.add(new Category(ID_6, "市场监察", R.mipmap.ic_7, Theme.green));
                categoryList.add(new Category(ID_4, "1#门交接班汇总报表", R.mipmap.ic_5, Theme.red));
                categoryList.add(new Category(ID_5, "车辆离场交接班", R.mipmap.ic_6, Theme.purple));
                categoryList.add(new Category(ID_7, "监察交接班报表", R.mipmap.ic_8, Theme.yellow));
                categoryList.add(new Category(ID_8, "二次离场登记", R.mipmap.ic_4, Theme.red));
                break;
        }
    }

    @Override
    public void workAgainSuccess(DutyBean result) {
        mDutyBean.count = result.count;
        mDutyBean.direction = result.direction;
        mDutyBean.editTime = result.editTime;
        mDutyBean.editUserId = result.editUserId;
        mDutyBean.logoType = result.logoType;
        mDutyBean.syncTime = result.syncTime;
        mDutyBean.validity = result.validity;
        startWorkSuccess(mDutyBean);
    }

    @Override
    public void setFirstWork(String returnMsg) {
        mTvContent.setText(returnMsg);
        mBtnAgainWork.setEnabled(false);
        mBtnWork.setEnabled(true);
        mBtnAgainWork.setBackgroundResource(R.color.color_e4e4e4);
        mBtnWork.setBackgroundResource(R.color.color_3793EF);
        mSpinnerPlace.setEnabled(true);
        mSpinnerTime.setEnabled(true);
    }

    @Override
    public void loadPositionListSuccess(ArrayList<NetWorkAreaBean> workAreaList, int i) {
        if (workAreaList != null && !workAreaList.isEmpty()) {
            mWorkAreaList.clear();
            workTimeList.clear();
            mWorkAreaList.addAll(workAreaList);
            workAreaAdapter.notifyDataSetChanged();
            mPresenter.loadWork();
            String a = workAreaList.get(i).deptId;
            switch (a) {
                case "2031":
                    workTimeList.add(new WorkTimeBean("1", "早班: 07:00 ~ 15:00"));
                    workTimeList.add(new WorkTimeBean("2", "中班: 15:00 ~ 23:00"));
                    workTimeList.add(new WorkTimeBean("3", "晚班: 23:00 ~ 次日07:00"));
                    break;
                default:
                    workTimeList.add(new WorkTimeBean("1", "早班: 06:00 ~ 14:00"));
                    workTimeList.add(new WorkTimeBean("2", "中班: 14:00 ~ 22:00"));
                    workTimeList.add(new WorkTimeBean("3", "晚班: 22:00 ~ 次日06:00"));
                    break;
            }
            workTimeAdapter.notifyDataSetChanged();
        }
    }
    AlertDialog.Builder builder = null;
    @Override
    public void loadWorkSuccess(DutyBean result) {
        this.mDutyBean = result;
        User.getInstance().setCompanyAuthority(result.remarks);
        mTvContent.setText(MessageFormat.format("      您最近的一次操作时间是 ------  {0}，在{1}登录了【智慧农批云管理系统】。", DateUtils.toFormat(result.loginTime), result.gateName));
        switch (result.logoType) {
            case "1":
            case "3":
                mBtnAgainWork.setEnabled(true);
                mBtnWork.setEnabled(false);
                mBtnAgainWork.setBackgroundResource(R.color.color_FD9840);
                mBtnWork.setBackgroundResource(R.color.color_e4e4e4);
                for (int i = 0; i < mWorkAreaList.size(); i++) {
                    if (mWorkAreaList.get(i).gateNo.equals(result.gateNo)) {
                        mSpinnerPlace.setSelection(i);
                        break;
                    }
                }
                for (int i = 0; i < workTimeList.size(); i++) {
                    if (workTimeList.get(i).workTimeNo.equals(result.ondutyClazz)) {
                        mSpinnerTime.setSelection(i);
                        break;
                    }
                }
                mSpinnerPlace.setEnabled(false);
                mSpinnerTime.setEnabled(false);
                break;
            case "2":
                mBtnAgainWork.setEnabled(false);
                mBtnWork.setEnabled(true);
                mBtnAgainWork.setBackgroundResource(R.color.color_e4e4e4);
                mBtnWork.setBackgroundResource(R.color.color_3793EF);
                mSpinnerPlace.setEnabled(true);
                mSpinnerTime.setEnabled(true);
                break;
        }


        if(Float.valueOf(Tools.getStayTime1(result.loginTime))>10.0){

            if(builder==null&&!result.logoType.equals("2")) {
                builder = new AlertDialog.Builder(this);
                AlertDialog dialog=builder.setTitle("登录提醒:")
                        .setMessage("您登录的时间为"+Tools.getStayTime1(result.loginTime)+"小时,已超过正常班次时间,请下班后再登录。")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                exit();
                            }}).create();
                dialog.show();
                dialog.setCancelable(false);
            }

        }
    }
}
